from rest_framework.views import APIView
from rest_framework.response import Response
from tools.tools import auth_token, TokenFun, delete_dir
from tools.const import BASE_DIR, INVITE_CODE
from private_website.apps.app01 import models
from private_website.apps.app01 import ser

import os

# Create your views here.


class UserLogin(APIView):
    def post(self, request):
        # 登录
        dic = {'code': 200, 'msg': 'ok'}
        try:
            phone = request.data.get('phone')
            password = request.data.get('password')
            # print(request.META.get('HTTP_TOKEN'))
            user_obj = models.Users.objects.filter(phone=phone)
            if user_obj:
                user_obj = user_obj.filter(password=password)
                if user_obj:
                    dic['code'] = 100
                    dic['msg'] = '登录成功'
                    user_obj.update(is_login=1)
                    token = TokenFun(user_obj[0])
                    data = token.login()
                    dic['token'] = data
                    return Response(dic)
                dic['msg'] = '密码错误'
            else:
                dic['msg'] = '账号不存在'
        except Exception as e:
            dic['code'] = 200
            dic['msg'] = str(e)
        return Response(dic)


class UserRegister(APIView):
    def post(self, request):
        # 注册
        dic = {'code': 100, 'msg': 'ok'}
        try:
            user_ser = ser.UserSer(data=request.data)
            code = request.data.get('invite_code')
            if code == INVITE_CODE:
                if user_ser.is_valid():
                    obj = user_ser.save()
                    obj_ser = ser.UserInfoSer(obj)
                    dic['data'] = obj_ser.data
                    token_obj = TokenFun(obj)
                    token = token_obj.login()
                    dic['data']['token'] = token
                else:
                    dic['code'] = 200
                    dic['msg'] = list(user_ser.errors.values())[0][0]
            else:
                return Response({'code': 200, 'msg': '邀请码不正确'})
        except Exception as e:
            dic['code'] = 200
            dic['msg'] = str(e)
        return Response(dic)


class LoginOut(APIView):
    @auth_token
    def post(self, request):
        dic = {'code': 100, 'msg': 'ok'}
        user = models.Users.objects.filter(token__token=request.META.get('HTTP_TOKEN')).first()
        try:
            token_obj = TokenFun(user)
            token_obj.logout()
        except Exception as e:
            dic['code'] = 200
            dic['msg'] = str(e)
        return Response(dic)


class UserInfo(APIView):
    @auth_token
    def get(self, request):
        dic = {'code': 100, 'msg': 'ok'}
        user = models.Users.objects.filter(token__token=request.META.get('HTTP_TOKEN')).first()
        try:
            userInfo = ser.UserInfoSer(user)
            dic['data'] = userInfo.data
        except Exception as e:
            dic['code'] = 200
            dic['msg'] = str(e)
        return Response(dic)


class DestroyUser(APIView):
    @auth_token
    def post(self, request):
        dic = {'code': 100, 'msg': 'ok'}
        phone = request.data.get('phone')
        password = request.data.get('password')
        user = models.Users.objects.filter(token__token=request.META.get('HTTP_TOKEN'), phone=phone, password=password)
        try:
            if user:
                dir_path = BASE_DIR + '/static/' + user.first().username + '/'
                delete_dir(dir_path)
                user.delete()
            else:
                dic['code'] = 200
                dic['msg'] = '信息错误'
        except Exception as e:
            dic['code'] = 200
            dic['msg'] = str(e)
        return Response(dic)


class Test(APIView):
    def get(self, request):
        dic = {'code': 100, 'msg': 'ok'}
        try:
            dic['data'] = '测试成功'
        except Exception as e:
            dic['code'] = 200
            dic['msg'] = str(e)
        return Response(dic)